﻿using Comain.Orders;
using Common;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
using IService;
using IService.Rempos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;

namespace WdbApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IOrderService orders;
        private readonly IAlipayClient _client;
        private readonly IOptions<AlipayOptions> _optionsAccessor;

        public OrderController(IOrderService orders,IAlipayClient _client,IOptions<AlipayOptions> optionsAccessor)
        {
            this.orders = orders;
            this._client = _client;
            this._optionsAccessor = _optionsAccessor;
        }
        /// <summary>
        /// 显示订单
        /// </summary>
        /// <returns></returns>
        [HttpGet("show")]
        public APIResult<PageComm<List<OrderDTO>>> show(string? name, string? orderFrom, DateTime? orderIntime, int index, int size)
        {
            var result = orders.show(name, orderFrom, orderIntime, index, size);
            return new APIResult<PageComm<List<OrderDTO>>>("")
            {
                Data = result,
                Code = ResultCode.OK,
                Isucess = true,
                Message = "显示成功"
            };
        }

        [HttpPost("PagePay")]
        public async Task<IActionResult> PagePay(string orderId, decimal ordwrMoney)
        {
            var model = new AlipayTradePagePayModel
            {
                Body = $"当前订单编号为{orderId}，订单金额为{ordwrMoney}元",
                //订单标题
                Subject = $"订单编号为{orderId}的订单正在支付",
                //订单总金额
                TotalAmount = ordwrMoney.ToString(),
                //商户订单号
                OutTradeNo = orderId,
                //产品码 右键转定义复制注释最后面
                ProductCode = "FAST_INSTANT_TRADE_PAY"
            };
            var req = new AlipayTradePagePayRequest();
            //完成订单支付
            req.SetBizModel(model);
            req.SetNotifyUrl("http://s3c86ef2.natappfree.cc");
            req.SetReturnUrl("http://localhost:5269/show");
            var response = await _client.PageExecuteAsync(req, _optionsAccessor.Value);
            return Content(response.Body, "text/html", Encoding.UTF8);
        }
    }
}
